package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysProject;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysProjectService;
import com.base.vistter.iframe.template.MinioTemplate;
import com.base.vistter.iframe.utils.IoUtils;
import com.base.vistter.iframe.utils.UploadUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

@Controller
@Slf4j
public class ImageController {

    @Resource(name = "minioTemplate")
    private MinioTemplate minioTemplate;
    @Resource(name = "sysProjectService")
    private SysProjectService sysProjectService;

    @GetMapping("/image")
    public void image(String filePath, HttpServletResponse response) throws IframeException {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String fileExt = UploadUtils.getFileExt(filePath);
        response.setContentType("image/" + ("svg".equals(fileExt) ? "svg+xml": fileExt));
        IoUtils.fileToStream(filePath, response);
    }
    @RequestMapping("/base64")
    public Result<String> base64(@RequestParam(value = "filePath") String filePath) throws IframeException {
        InputStream inputStream = null;
        ByteArrayOutputStream out = null;
        try {
            inputStream = minioTemplate.downloadFile(filePath);
            out = new ByteArrayOutputStream();
            byte[] result;
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            result = out.toByteArray();
            String base64 = Base64.getEncoder().encodeToString(result);
            return Result.ok(base64);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return null;
    }

    @RequestMapping("/logo")
    public void logo(HttpServletResponse response) throws IframeException, IOException {
        SysProject sysProject = sysProjectService.selectOne();
        this.image(sysProject.getLogo(), response);
    }

    @RequestMapping("/icon")
    public void icon(HttpServletResponse response) throws IframeException, IOException {
        SysProject sysProject = sysProjectService.selectOne();
        this.image(sysProject.getIcon(), response);
    }

}
